<?php
/**
 * Name: 日志管理回调函数集合
 * User: join.leong
 * Date: 2016/3/10 16:07
 */

/**
 * 获取日志消耗道具
 * @param $info
 * @param $params
 * @return string
 */
function consume($info, $params): string
{
    $itemConf = getDiffConfig($params["config"] ?? "item");
    $data = "";
    //param为两个$info的key：格式code的key,num的key
    if (isset($params["num"])) {
        if ($itemConf[$info[$params["field"]]]) {
            $data .= $itemConf[$info[$params["field"]]] . ' * ' . $info[$params["num"]] . '<br/>';
        }
    } elseif ($costArr = json_decode($info[$params["field"]], true)) {
        foreach ($costArr as $key => $v) {
            if (isset($itemConf[$v])) {
                if (is_array($itemConf[$v])) {
                    $name = $itemConf[$v][$params["config_field"] ?? "name"];
                } else {
                    $name = $itemConf[$v];
                }
            } else {
                $name = $v;
            }
            $data .= $name . '<br/>';
        }
    } //param为一个$info的key,key 的格式为：[{code,num},{code,num}]
    else {
        $consumeArr = string2array($info[$params["field"]]);
        foreach ($consumeArr as $key => $v) {
            $flag = $v[1] ? ' * ' : '';
            $data .= ($itemConf[$v[0]] ?? $v[0]) . $flag . $v[1] . '<br/>';
        }
    }
    return rtrim($data, '<br/>');
}

/**
 * 返回成功或者失败1-成功；2-失败
 * @param $info
 * @param $param
 * @return string
 */
function result($info, $param): string
{
    //切割传入的参数key,key2,key3
    $paramArr = explode(',', $param);
    if ($info[$paramArr[0]] == 1)
        return '<span style="color:#006600">' . L("成功") . '</span>';
    else
        return '<span style="color:#999999">' . L("失败") . '</span>';
}

/**
 * 把时间戳转成一个时间
 * @param $info
 * @param $params
 * @return bool|string
 */
function changeDate($info, $params = [])
{
    return date("Y-m-d H:i:s", $info[$params["field"]]);
}


function changeAttach($info, $params = [])
{
    $itemConf = getDiffConfig("item");
    $consumeArr = string2array($info[$params["field"]]);
    //    \think\facade\Log::write("debug consumeArr" . json_encode($consumeArr));
    $j = "";
    foreach ($consumeArr as $v) {
        $itemName = $itemConf[$v[0]] ?? $v[0];
        //        \think\facade\Log::write("itemName" . $itemName);
        $j .= $itemName . " * " . $v[1] . "<br/>";
    }
    return $j;
}


function economyName($config, $id)
{
    return $config[$id] ?? $id;

}


function formatData($info, $field)
{
    // Log::write("debug formatData input: " . var_export($info, true)."field".var_export($field));
    $items = explode(',', $info['loss_hero_ids']);
    $items = array_filter($items, function ($item) {
        return $item !== '';
    });
    // Log::write("debug formatData input: " . var_export($items, true));

    $result = [];
    for ($i = 0; $i < count($items); $i += 3) {
        try {
            $baseId = isset(explode(':', $items[$i])[1]) ? explode(':', $items[$i])[1] : 0;

            $star = explode(':', $items[$i + 1])[1];
            $lev = explode(':', $items[$i + 2])[1];
            $heroconfig = getHeroConfig($baseId);
            $result[] = $heroconfig["name"] . '_星级:' . $star . '_等级:' . $lev;
        } catch (\Exception $e) {
            $result[] = json_encode($items, true);
        }

    }

    return implode(';', $result);
}


function formatItemData($info, $field)
{
    // Log::write("debug formatData input: " . var_export($info, true)."field".var_export($field));
    $items = $info['assets'];
    $items .= $info["items"];

    $result = [];
    preg_match_all('/base_id:(\d+),Value:(\d+),?/', $items, $matches, PREG_SET_ORDER);

    foreach ($matches as $match) {
        $result[$match[1]] = $match[2];
    }

    $res =[];
    foreach($result as $key =>$val){
        $itemConf = getDiffConfig('item');
        $name = $itemConf[$key] ?? $key;
        $res[] = $name . ' * ' . $val;
    }

    return implode(';', $res);
}


function GainTypeName($info, $field)
{
    $configArr = config("operate_conf");
    $S_TYPE = $info["s_type"];
    return $configArr[$S_TYPE] ?? $S_TYPE;
}

/**
 * 获取物品名称
 * @param $info
 * @param $params
 * @return bool|string
 */
function itemName($info, $params)
{
    static $itemConf = array();
    $config = $params["config"];
    $field = $params["field"];
    //加载物品配置
    if (!isset($itemConf[$config])) {
        $itemConf[$config] = getDiffConfig($config);
    }
    if (empty($info[$field])) {
        return "无";
    }
    if (isset($itemConf[$config][$info[$field]])) {
        if (is_array($itemConf[$config][$info[$field]])) {
            return $itemConf[$config][$info[$field]][$params["config_field"]];
        } else {
            return $itemConf[$config][$info[$field]];
        }
    } else {
        return $info[$field];
    }
}

/**
 * 根据传入的枚举字段，获取对应的枚举值
 * @param $info
 * @param $params
 * @return mixed
 */
function getTypeName($info, $params)
{
    static $itemConf = array();
    $key = $params["config"];
    //加载配置
    if (empty($itemConf[$key])) {
        $itemConf[$key] = config($key);
    }
    if (isset($params["diff_config"])) {
        $index = itemName($info, ['field' => $params["field"], 'config' => $params["diff_config"], 'config_field' => $params["diff_config_field"]]);
    } else {
        $index = $info[$params["field"]];
    }
    if (isset($itemConf[$key][$index])) {
        if (is_array($itemConf[$key][$index])) {
            return $itemConf[$key][$index]["name"];
        } else {
            return $itemConf[$key][$index];
        }
    } else {
        return $index;
    }
}

/**
 * 根据当前值和消耗值得到剩余值
 * @param $info
 * @param $param    #当前值,消耗值
 * @return mixed
 */
function residue($info, $param)
{
    //切割参数(消耗前值,消耗值)
    $paramArr = explode(",", $param);
    $newNum = 0;
    //如果消耗是一个数字
    if (is_numeric($info[$paramArr[1]])) {
        $newNum = $info[$paramArr[0]] - $info[$paramArr[1]];
    } else {
        //如果消耗是一个字符串，可以解析成数组
        $consumeArr = string2array($info[$paramArr[1]]);
        if (is_array($consumeArr)) {
            //消耗数量
            $consumeNum = $consumeArr[0][1];
            $newNum = $info[$paramArr[0]] - $consumeNum;
        }
    }
    return abs($newNum);
}

/**
 * 获取行为类型名称
 * @param $info
 * @param array $params
 * @return bool|string
 */
function gainName($info, array $params = [])
{
    static $itemConf = array();
    //加载物品配置
    if (empty($itemConf)) {
        $itemConf = getDiffConfig('origin');
    }
    return $itemConf[$info[$params["field"]]] ?? $info[$params["field"]];
}



function getLogType($key)
{
    static $logtypeConf = array();
    //加载物品配置
    if (empty($logtypeConf)) {
        $logtypeConf = getDiffConfig('log_type');
    }
    return $logtypeConf[$key] ?? $key;
}
/**
 * 组装转生等级
 * @param $info
 * @param $param
 * @return string
 */
function getLevel($info, $param): string
{
    $paramArr = explode(",", $param);
    return $info[$paramArr[0]] . "转" . $info[$paramArr[1]] . "级";
}

/**
 * 获取职业名称
 * @param $info
 * @param $param
 * @return mixed
 */
function getCareer($info, $param)
{
    $careerConf = C("GAME_CAREER");
    return $careerConf[$info[$param]];
}